'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 竞技场
 * @author agl
 */

var _ = require('lodash');
var dataApi = require('../libs/dataApi');
var constantUtil = require('../util/constantUtil');

/**
 * 按副本ID取出索引号
 */
exports.getMonsterIndexById = function (monster_id) {
    var index = 0;
    var found_idx = 0;
    dataApi.monster_config.any(function (item) {
        if (item.id === monster_id) {
            found_idx = index;
            return true;
        }
        index++;
    });
    return found_idx;
};

/**
 * 取出下一个副本ID
 */
exports.getNextMonsterId = function (monster_id) {
    var conf = dataApi.monster_config.find(function (o) {
        return o.id > monster_id;
    });

    if (!!conf) {
        return conf.id;
    }

    return 99999;
};

/**
 * 取出宝箱奖励信息
 */
exports.getBoxReward = function (monster_id) {
    return dataApi.monster_reward.findById(monster_id);
};

/**
 * 取出商城物品信息
 */
exports.getShopItem = function (shop_id) {
    return dataApi.monster_shop.findById(shop_id);
};

var shop_rate_info = [];
var total_prob = 0;
var must_appear_list = [];
var loaded = false;

var _loadShopInfo = function () {
    if (loaded) {
        return;
    }
    loaded = true;
    shop_rate_info = [];
    total_prob = 0;
    var shop_conf = {};
    var shop_pos = 0;
    dataApi.monster_shop.any(function (item) {
        if (!!item.must_appear) {
            shop_pos++;
            must_appear_list.push({
                shop_pos: shop_pos,
                shop_id: item.id,
                count: item.buy_time
            });
        } else {
            total_prob += item.probability;
            shop_rate_info.push({
                shop_id: item.id,
                sum_rate: total_prob,
                count: item.buy_time
            });
        }
    });

    var monster_shop_num = constantUtil.value('monster_shop_num');
    if (must_appear_list.length > monster_shop_num) {
        must_appear_list = _.slice(must_appear_list, 0, monster_shop_num);
    }
};

/**
 * 获取商城物品概率信息
 * @return {[type]}     [description]
 */
exports.getShopRateInfo = function () {
    _loadShopInfo();
    return shop_rate_info;
};

/**
 * 获取商城物品概率信息
 * @return {[type]}     [description]
 */
exports.getTotalProb = function () {
    _loadShopInfo();
    return total_prob;
};

/**
 * 取出商城物品信息
 */
exports.getMustAppearList = function (shop_id) {
    _loadShopInfo();
    return must_appear_list;
};
